package com.tarena.lbs.dao;

import com.tarena.lbs.entity.User;
import com.tarena.lbs.dao.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public class UserDao {

    @Autowired
    private UserMapper userMapper;

    public User getById(Long id) {
        return userMapper.selectById(id);
    }

    public User getByUsername(String username) {
        return userMapper.selectByUsername(username);
    }

    public List<User> getAll() {
        return userMapper.selectAll();
    }

    public int save(User user) {
        return userMapper.insert(user);
    }

    public int update(User user) {
        return userMapper.update(user);
    }

    public int delete(Long id) {
        return userMapper.deleteById(id);
    }

    public List<User> getNearbyUsers(Double latitude, Double longitude, Double distance) {
        return userMapper.selectNearbyUsers(latitude, longitude, distance);
    }

    public int insert(User user) {
        Date now = new Date();
        user.setCreateTime(now);
        user.setUpdateTime(now);
        return userMapper.insert(user);
    }
}
